package cn.vawn.exam.controller;

import cn.vawn.base.consts.AppConsts;
import cn.vawn.base.controller.BaseController;
import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.ResponseResult;
import cn.vawn.base.util.DateUtil;
import cn.vawn.exam.entity.LocalUniterm;
import cn.vawn.exam.service.ILocalUnitermService;
import cn.vawn.exam.service.exception.DataNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("exam")
public class LocalUnitermController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(LocalUnitermController.class);

	@Resource
	private ILocalUnitermService localUnitermService;
	
	@RequestMapping(value = "/getAllLocalUniterm", method = RequestMethod.GET)
	@ResponseBody
	public ResponseResult<PageDTO> getAllLocalUniterm(HttpServletRequest request, HttpServletResponse response){
//		System.out.println(request.getParameter("token"));
//		List<LocalUniterm> localUniterm

		logger.info("try to get all local uniterm");

		PageDTO unitermList = localUnitermService.getAllLocalUniterm(getPage());

		//
		ResponseResult<PageDTO> rr =
				new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, unitermList);

		return rr;
	}

	@RequestMapping(value = "/getPartlLocalUniterm", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<PageDTO> getPartLocalUniterm(
			HttpServletRequest request, HttpServletResponse response) throws ParseException {
		//token验证
		String token = request.getParameter("token");
		String localUnitermName = request.getParameter("localUnitermName");
		String code = request.getParameter("code");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		logger.info("try to get local unitrm by localUnitermName = " + localUnitermName + ",code = " +
						code + ",startTime = " + startTime + ", endTime = " + endTime);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date startDate = null;
		if(startTime != null && !"".equals(startTime)) {
			startDate = sdf.parse(startTime);
		}
		System.out.println(endTime);
		Date endDate = null;
		if(endTime != null && !("".equals(endTime))) {
			endDate = sdf.parse(endTime);
		} else {
			endDate = new Date();
		}

		PageDTO unitermList = localUnitermService.getPartLocalUniterm(getPage(), localUnitermName, code, startDate,endDate);

		//
		ResponseResult<PageDTO> rr =
				new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, unitermList);

		return rr;
	}

	@RequestMapping(value = "/dltLocalUniterm", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> dltLocalUniterm(HttpServletRequest request) {
		//token��֤
//		String token = request.getParameter("token");
		String localUnitermId = request.getParameter("localUnitermId");

		logger.info(getCurrentUser().getUsername() + "try to delete local uniterm by localUnitermId = " + localUnitermId
					+ "at time : " + DateUtil.formatDate(new Date()));

		ResponseResult<Void> rr = null;
		try {
			localUnitermService.dltLocalUnitermByid(Integer.parseInt(localUnitermId));
			rr = new ResponseResult<Void>(1, "删除成功");
		} catch (DataNotFoundException e) {
			rr = new ResponseResult<Void>(0, e);
		}

		return rr;
	}

	@RequestMapping(value = "/updateLocalUniterm", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> updateLocalUniterm(HttpServletRequest request, HttpServletResponse response) {
		//token验证
//		String token = request.getParameter("token");
//		System.out.println(token);

		String localUnitermName = request.getParameter("localUnitermName");
		String localUnitermId = request.getParameter("localUnitermId");
//		System.out.println(localUnitermId + "1111111111");
		String code = request.getParameter("code");
		String price = request.getParameter("price");
		String brief = request.getParameter("brief");

		logger.info("try to update local uniterm by localUnitermName = " + localUnitermName +
					",localUnitermId = " + localUnitermId + ",code = " + code + ",price = " +
					price + ",brief = " + brief);

		LocalUniterm localUniterm = new LocalUniterm();
		localUniterm.setName(localUnitermName);
		localUniterm.setBrief(brief);
		localUniterm.setCode(code);
		localUniterm.setId(Integer.parseInt(localUnitermId));

		ResponseResult<Void> rr = null;

		try {
			localUniterm.setPrice(getDatafromString(price));
		} catch (NumberFormatException e) {
			rr = new ResponseResult<>(0,"价格输入错误，请输入正确的格式");
		}

		try {
			localUnitermService.updateLocalUnitermById(localUniterm);
			rr = new ResponseResult<Void>(1, "更新成功");
		} catch (DataNotFoundException e) {
			rr = new ResponseResult<Void>(0, e);
		}
		
		return rr;
	}	
	
	@RequestMapping(value = "/addLocalUniterm", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> addLocalUniterm(HttpServletRequest request, HttpServletResponse response) {
		//token��֤
		String token = request.getParameter("token");
		System.out.println(token);

		String localUnitermName = request.getParameter("localUnitermName");
		String code = request.getParameter("code");
		String price = request.getParameter("price");
		String brief = request.getParameter("brief");

		logger.info("try to add local uniterm by localUnitermName = " + localUnitermName +
					",code = " + code + ",price = " + price + ",nrief = " + brief);
//		System.out.println(localUnitermName + "***" + code + "***" + price + "***" + brief);

		LocalUniterm localUniterm = new LocalUniterm();
		localUniterm.setName(localUnitermName);
		localUniterm.setBrief(brief);
		localUniterm.setCode(code);

		ResponseResult<Void> rr = null;
		try {
			localUniterm.setPrice(getDatafromString(price));
		} catch (NumberFormatException e) {
			rr = new ResponseResult<>(0,"价格输入错误，请输入正确的格式");
		}

		int num = localUnitermService.addLocalUniterm(localUniterm);

		if(num == 1) {
			rr = new ResponseResult<Void>(num, "添加成功");
		}else if (num == 0){
			rr = new ResponseResult<Void>(num, "添加失败");
		}

		return rr;
	}

	@RequestMapping(value = "/getLocalUniterm", method = RequestMethod.GET)
	@ResponseBody
	public ResponseResult<LocalUniterm> getLocalUniterm(HttpServletRequest request, HttpServletResponse response) {
		//token验证
		String token = request.getParameter("token");
		String id = request.getParameter("localUnitermId");

		logger.info("get local uniterm by localUnitermId =" + id);
//		System.out.println(token);
//		System.out.println(id);

		ResponseResult<LocalUniterm>  rr = null;

		try {
			LocalUniterm localUniterm = localUnitermService.getLocalUniterm(Integer.parseInt(id));
			rr = new ResponseResult<>(1, localUniterm);
		} catch (DataNotFoundException e) {
			rr = new ResponseResult<>(0,e.getMessage());
		}

		return rr;
	}

	@RequestMapping(value = "/localUniterm", method = RequestMethod.GET)
	@ResponseBody
	public ResponseResult<List<LocalUniterm>> getLocalUnitermByIds(@RequestParam("localUnitermIds") Integer[] localUnitermIds) {
		logger.info("try to get localUniterm by localUnitermIds: " + localUnitermIds);
		List<LocalUniterm> localUnitermList = localUnitermService.getLocalUnitermByIds(localUnitermIds);
		ResponseResult<List<LocalUniterm>> rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, localUnitermList);
		return rr;
	}

	//将字符串转换成int类型
	public int getDatafromString (String source) {
		int price = 0;
		if(source.indexOf('.') != -1) {
			double cast = Double.parseDouble(source);
			price = (int) (cast * 100);
		} else {
			int cast = Integer.parseInt(source);
			price = cast * 100;
		}

		return price;
	}

	//将int类型转换成double型字符串
	public  String parseStr(int price) {

		String cast = price / 100 + "." + price % 100;

		return cast;
	}

}
